package com.yuwenxin.core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDaoImpl implements BaseDao {

	@Override
	public int insert(Object object) throws Exception {
		Connection con = DBUtil.getConnection();
		String sql = getInsertSql(object);
		PreparedStatement pst = con.prepareStatement(sql);
		int rows = pst.executeUpdate();
		DBUtil.close(con, pst);
		return rows;
	}

	@Override
	public int update(Object object) throws Exception {
		String sql = getUpdateSql(object);
		if (sql.equals("") || sql ==null){
			return 0;
		}
		Connection con = DBUtil.getConnection();
		PreparedStatement pst = con.prepareStatement(sql);
		int rows = pst.executeUpdate();
		DBUtil.close(con,pst);
		return rows;
	}

	@Override
	public int delete(int id) throws Exception {
		String sql = getDeleteSql();
		if (sql.equals("") || sql ==null){
			return 0;
		}
		Connection con = DBUtil.getConnection();
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setString(1, String.valueOf(id));

		int num = pst.executeUpdate();
		DBUtil.close(con,pst);
		return num;
	}

	@Override
	public List findAll() throws Exception {

		Connection con  = DBUtil.getConnection();
		String sql = getSelectAllSql();
		if (sql.equals("") || sql ==null){
			return null;
		}
		List<Object> list = new ArrayList<>();
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()){
			Object obj;
			obj = toEntity(rs);
			list.add(obj);
		}
		DBUtil.close(con, stmt, rs);
		return list;
	}

	@Override
	public List fuzzyFind(String name) throws Exception {
		String sql = getFuzzyFindSql();
		Connection con = DBUtil.getConnection();
		if (sql.equals("") || sql ==null){
			return null;
		}
		List<Object> list = new ArrayList<>();
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setString(1, "%"+name+"%");

		ResultSet rs =pst.executeQuery();
		while (rs.next()){
			Object obj;
			obj = toEntity(rs);
			list.add(obj);
		}
		DBUtil.close(con, pst, rs);
		return list;
	}

	@Override
	public Object find(int id) throws Exception {
		String sql = getSelectByIdSql();
		if (sql.equals("") || sql ==null){
			return null;
		}
		Object obj;

		Connection con = DBUtil.getConnection();
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setString(1, String.valueOf(id));

		ResultSet rs =pst.executeQuery();
		if (!rs.next()){
			return null;
		}else {
			obj = toEntity(rs);
		}
		DBUtil.close(con,pst,rs);
		return obj;
	}

	public Object findByName(String name) throws Exception {
		String sql = getSelectByNameSql();
		if (sql.equals("") || sql ==null){
			return null;
		}
		Object obj;

		Connection con = DBUtil.getConnection();
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setString(1, name);

		ResultSet rs =pst.executeQuery();
		if (!rs.next()){
			return null;
		}else {
			obj = toEntity(rs);
		}
//		while (rs.next()){
//		}
		DBUtil.close(con,pst,rs);
		return obj;
	}

	@Override
	public List findByCondition(BaseQueryModel queryModel) throws Exception {
		Connection con  = DBUtil.getConnection();
		String sql = getConditionSql(queryModel);
		PreparedStatement pst = con.prepareStatement(sql);
		
		ResultSet rs = pst.executeQuery();
		
		List<Object> list =new ArrayList<>();
		while(rs.next()){
			Object obj = toEntity(rs);
			list.add(obj);
		}
		
		DBUtil.close(con, pst, rs);
		return list;
	}

	//通过抽象函数获取sql语句，提高代码复用性
	public abstract String getSelectAllSql();
	public abstract String getSelectByNameSql();
	public abstract String getFuzzyFindSql();  // 模糊查询
	public abstract String getUpdateSql(Object object) throws Exception;
	public abstract String getDeleteSql();
	public abstract String getSelectByIdSql();
	public abstract String getInsertSql(Object object);
	public abstract String getConditionSql(BaseQueryModel queryModel);
	public abstract Object toEntity(ResultSet rs) throws Exception;  // 该函数用于find查找数据库后返回一个java对象
}
